/**
 * Copyright (c) 2018 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.renren.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.renren.config.SysConfig;
import io.renren.entity.ClassEntity;
import io.renren.entity.DatabaseEnum;
import io.renren.service.SysGeneratorService;
import io.renren.utils.PageUtils;
import io.renren.utils.Query;
import io.renren.utils.R;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.NumberUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;

/**
 * 代码生成器
 * 
 * @author Mark sunlightcs@gmail.com
 */
@Controller
public class SysGeneratorController {
	@Autowired
	private SysGeneratorService sysGeneratorService;
	@Autowired
	private SysConfig sysConfig;
	
	/**
	 * 列表
	 */
	@ResponseBody
	@RequestMapping("/sys/generator/list")
	public R list(@RequestParam Map<String, Object> params){
		PageUtils pageUtil = sysGeneratorService.queryList(new Query(params));
		
		return R.ok().put("page", pageUtil);
	}
	
	/**
	 * 生成代码
	 */
	@RequestMapping("/sys/generator/code")
	public void code(@RequestParam Map<String, Object> params, HttpServletResponse response) throws IOException{
		String tables =( String)params.get("tables");
		String paramJson =  JSON.toJSONString(params);
		ClassEntity entity = JSON.parseObject(paramJson,ClassEntity.class);
		byte[] data = sysGeneratorService.generatorCode(entity);
		
		response.reset();  
        response.setHeader("Content-Disposition", "attachment; filename=\"renren.zip\"");  
        response.addHeader("Content-Length", "" + data.length);  
        response.setContentType("application/octet-stream; charset=UTF-8");  
  
        IOUtils.write(data, response.getOutputStream());  
	}
	/**
	 * 列表
	 */
	@ResponseBody
	@RequestMapping("/sys/config/list")
	public R configList(@RequestParam Map<String, Object> params){
		PageUtils page = new PageUtils(Arrays.asList(sysConfig),1,1,1);
		return R.ok().put("page", page);
	}

	/**
	 * 列表
	 */
	@ResponseBody
	@RequestMapping("/sys/config/update")
	public  R configUpdate(@RequestParam Map<String, Object> params){
		if(null == params || params.isEmpty()){
			return R.error(100,"数据,不能为空");
		}
		String json = JSON.toJSONString(params);
		SysConfig sys = JSON.parseObject(json,SysConfig.class);
		String database = DatabaseEnum.getValue(sys.getDatabaseType());
		if(null == database ){
			return R.error(100,"暂时不支持该数据库类型");
		}
		BeanUtils.copyProperties(sys,sysConfig);
		if(sys.getIsRedisCache() > 2 ){
			return R.error(100,"暂时不支持该redis类型");
		}
		if(sys.getJdbcOrMybatis() == 1 || sys.getJdbcOrMybatis() == 2){
			return R.error(100,"暂时不支持该数据关系类型");
		}

		System.out.println(sysConfig.toString());
		return R.ok();

	}

}
